//
//  StoryDetailViewController.m
//  BreadTravel
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "StoryDetailViewController.h"
#import "StoryDetailTableViewCell.h"
#import "CJTStoryDetailHeaderView.h"
#import "StoryDetailModel.h"
#import "FXBlurView.h"

@interface StoryDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *array;
@property(nonatomic, strong)NSMutableArray *headArray;
@property(nonatomic, copy)NSString *sectionName;
@property(nonatomic, strong)NSDictionary *userDic;
@property(nonatomic, assign)CGFloat headerHeight;
@property(nonatomic, strong)UIImageView *backImageView;
@property(nonatomic, retain)FXBlurView *blurView;

@end

@implementation StoryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // self.urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@",self.urlStr];2387667128
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    self.headArray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    self.userDic = [NSDictionary dictionary];
//    self.tabBarController.tabBar.translucent = NO;
    self.navigationController.navigationBar.translucent = NO;
    [self createView];
    
}
//返回
-(void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}
-(void)createView
{
    self.tableView = [[UITableView alloc] init];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:(UITableViewStyleGrouped)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    
    [self.tableView registerClass:[StoryDetailTableViewCell class] forCellReuseIdentifier:@"cell"];

    [self getData];
}

-(void)createTableHeaderView
{
    self.imageURL = self.userDic[@"avatar_l"];
    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, WIDTH, 200)];
    self.backImageView.backgroundColor = [UIColor redColor];
    [self.tableView addSubview:self.backImageView];
    
    
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:self.imageURL] placeholderImage:IMAGE];
    self.blurView = [[FXBlurView alloc] initWithFrame:self.view.frame];
    [self.backImageView addSubview:self.blurView];
    self.blurView.blurRadius = 15;
    
    [self.backImageView addSubview:self.blurView];
    
    [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.backImageView);
    }];

    
//    CJTStoryDetailHeaderView *view = [[CJTStoryDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    CJTStoryDetailHeaderView *view = [[CJTStoryDetailHeaderView alloc] init];
    
    
    NSURL *url = [NSURL URLWithString:self.userDic[@"avatar_l"]];
    [view.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
    view.label.text = self.userDic[@"name"];

    [self.backImageView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        
//       make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(100, 100, 100, 100));
        make.bottom.equalTo(self.backImageView.mas_bottom);
        make.left.equalTo(self.backImageView.mas_left);
        make.right.equalTo(self.backImageView.mas_right);
        make.height.mas_equalTo(@100);
        
    }];
    
    
    
}


-(void)getData
{
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@",self.urlStr];
//    2387667128
    
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSDictionary *bigDic = result;
        NSDictionary *dataDic = [bigDic objectForKey:@"data"];
        NSDictionary *tripDic = [dataDic objectForKey:@"trip"];
        
        self.userDic = [tripDic objectForKey:@"user"];
        NSDictionary *spotDic = [dataDic objectForKey:@"spot"];
        self.sectionName = [spotDic objectForKey:@"text"];
        
        CGSize size1 = [SizeforLabel labelRectWithSize:CGSizeMake(WIDTH-40, MAXFLOAT) LabelText:self.sectionName Font:[UIFont systemFontOfSize:20.0f]];
        self.headerHeight = size1.height;
        NSMutableArray *array = [spotDic objectForKey:@"detail_list"];
        for (NSDictionary *dic in array) {
            
            StoryDetailModel *model = [[StoryDetailModel alloc] initWithDictionary:dic];

            CGSize digestHeight = [SizeforLabel labelRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT) LabelText:model.text Font:[UIFont systemFontOfSize:16.0f]];
            
            model.height = digestHeight.height;
            
            [self.array addObject:model];
            
        }
        [self createTableHeaderView];
        [self.tableView reloadData];
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        
    }];

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StoryDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.model = self.array[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StoryDetailModel *model = self.array[indexPath.row];
    
    CGSize size = [StoryDetailTableViewCell heightForImageModelWidth:model.photo_width ModelHeight:model.photo_height];
    CGFloat height = size.height;
    
    return model.height + height + 35;
}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CJTStoryDetailHeaderView *view = [[CJTStoryDetailHeaderView alloc] init];
    [view.headImageView removeFromSuperview];
    [view.lineView removeFromSuperview];
    view.label.text = self.sectionName;
    view.label.numberOfLines = 0;
    
    
    view.label.textAlignment = NSTextAlignmentCenter;
    view.label.font = [UIFont systemFontOfSize:20];
    
    [view.label mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(10);
            make.left.mas_equalTo(@20);
            
            make.right.mas_equalTo(@(-20));
        
    }];
    
    
    return view;
}
////section头  高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.headerHeight+20;
//    return 100;
}




//让section头 不悬停
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    NSLog(@"y2 === %f",y);
    
    if (y < -200) {
        CGRect frame = self.backImageView.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.backImageView.frame = frame;

        self.blurView.blurRadius = y+215;
        if (y < -215) {
            self.blurView.blurRadius = 0;
        }
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end


